package cn.swenty.backstage.framework.common;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * SpringMvc 
 * 把请求的所有参数封装到Map中,提供最常用的方法
 * date：2015-03-21
 * @version 3.0
 */
public class FormMap extends HashMap<String, Object> implements Serializable  {

	private static final long serialVersionUID = 1L;
	
	public FormMap set(String key,Object value){
		super.put(key.toLowerCase(), value);
		return this;
	}
	public Object put(String key, Object value) {
		return super.put(key.toLowerCase(), value);
	}
	
	public Object get(Object key){
		return super.get(key.toString().toLowerCase());
	}
	@Override
	public void putAll(Map<? extends String, ? extends Object> m) {
		Iterator iterator = m.keySet().iterator();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			this.put(key.toString(), m.get(key));
			
		}
	}
	/**
	 * Get attribute of mysql type: varchar, char, enum, set, text, tinytext,
	 * mediumtext, longtext
	 */
	public String getStr(String attr) {
		return (String) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of mysql type: int, integer, tinyint(n) n > 1, smallint,
	 * mediumint
	 */
	public Integer getInt(String attr) {
		return (Integer) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of mysql type: bigint, unsign int
	 */
	public Long getLong(String attr) {
		return (Long) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of mysql type: unsigned bigint
	 */
	public java.math.BigInteger getBigInteger(String attr) {
		return (java.math.BigInteger) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of mysql type: date, year
	 */
	public java.util.Date getDate(String attr) {
		return (java.util.Date) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of mysql type: time
	 */
	public java.sql.Time getTime(String attr) {
		return (java.sql.Time) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of mysql type: timestamp, datetime
	 */
	public java.sql.Timestamp getTimestamp(String attr) {
		return (java.sql.Timestamp) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of mysql type: real, double
	 */
	public Double getDouble(String attr) {
		BigDecimal bigDecimal = this.getBigDecimal(attr.toLowerCase());
		if(bigDecimal != null)return bigDecimal.doubleValue();
		return null;
	}

	/**
	 * Get attribute of mysql type: float
	 */
	public Float getFloat(String attr) {
		BigDecimal bigDecimal = this.getBigDecimal(attr.toLowerCase());
		if(bigDecimal != null)return bigDecimal.floatValue();
		return null;
	}

	/**
	 * Get attribute of mysql type: bit, tinyint(1)
	 */
	public Boolean getBoolean(String attr) {
		return (Boolean) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of mysql type: decimal, numeric
	 */
	public java.math.BigDecimal getBigDecimal(String attr) {
		Object o = this.get(attr.toLowerCase());
		if(o == null){
			return null;
		}else if(o instanceof Integer){
			return new BigDecimal((Integer)o);
		}else if(o instanceof Float){
			return new BigDecimal((Float)o);
		}else if(o instanceof Double){
			return new BigDecimal((Double)o);
		}else if(o instanceof Long  ){
			return new BigDecimal((Long)o);
		}else if(o instanceof String  ){
			return new BigDecimal((String)o);
		}else{
			return (java.math.BigDecimal) this.get(attr.toLowerCase());
		}
	}

	/**
	 * Get attribute of mysql type: binary, varbinary, tinyblob, blob,
	 * mediumblob, longblob
	 */
	public byte[] getBytes(String attr) {
		return (byte[]) this.get(attr.toLowerCase());
	}

	/**
	 * Get attribute of any type that extends from Number
	 */
	public Number getNumber(String attr) {
		return (Number) this.get(attr.toLowerCase());
	}
	
	/**
	 * Return attribute names of this model.
	 */
	public String[] getAttrNames() {
		Set<String> attrNameSet =this.keySet();
		return attrNameSet.toArray(new String[attrNameSet.size()]);
	}

	/**
	 * Return attribute values of this model.
	 */
	public Object[] getAttrValues() {
		Collection<Object> attrValueCollection = values();
		return attrValueCollection.toArray(new Object[attrValueCollection
				.size()]);
	}

}
